package com.lcc.lynx.base.system.util;

import com.lcc.lynx.base.system.pojo.dv.SysConfigDV;
import com.lcc.lynx.base.system.pojo.dv.SysDictDV;
import com.lcc.lynx.base.system.pojo.dv.SysDictDtlDV;
import com.lcc.lynx.base.system.service.SysConfigService;
import com.lcc.lynx.base.system.service.SysDictService;
import com.lcc.lynx.common.cache.enums.CacheEnum;
import com.lcc.lynx.common.cache.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class CacheUtil {

    private static CacheService cacheService;

    private static Environment env;
    private static SysConfigService configService;
    private static SysDictService dictService;

    @Autowired
    public CacheUtil(Environment env,
                      SysConfigService configService,
                      SysDictService dictService) {
        CacheUtil.env = env;
        CacheUtil.configService = configService;
        CacheUtil.dictService = dictService;
    }

    /**
     * 注入cacheService实现，如果有的话
     * @param cacheService
     */
    @Autowired
    public void setCacheService(CacheService cacheService) {
        CacheUtil.cacheService = cacheService;
    }

    /**
     * 没有注入成功的话 使用默认的redis服务
     * @return
     */
    public static CacheService getCacheService() {
        return cacheService;
    }

    public static List<SysDictDtlDV> getDict(String code){
        List<SysDictDtlDV> list = getCacheService().get(CacheEnum.dict + code);
        if(list == null){
            SysDictDV sysDictDV = dictService.getByCode(code);
            if(sysDictDV != null){
                list = sysDictDV.getDtlDVList();
                getCacheService().set(CacheEnum.dict + code, list, 30*24*60*60, TimeUnit.SECONDS);
            }
        }
        return list;
    }

    public static void setDict(String code, List<SysDictDtlDV> list){
        getCacheService().set(CacheEnum.dict + code, list, 30*24*60*60, TimeUnit.SECONDS);
    }

    public static String getConfig(String key){
        String groupKey = CacheEnum.config + key;
        String val = getCacheService().get(groupKey);
        if(val != null){
            return val.toString();
        }
        val = env.getProperty(key);
        if(val != null){
            getCacheService().set(groupKey, val, 24*60*60, TimeUnit.SECONDS);
            return val.toString();
        }

        SysConfigDV p = new SysConfigDV();
        p.setCfgKey(key);
        SysConfigDV sysConfigDV = configService.selectOne(p);
        if(sysConfigDV != null){
            getCacheService().set(groupKey, val, 24*60*60, TimeUnit.SECONDS);
            return sysConfigDV.getCfgValue();
        }
        return null;
    }
    public static void setConfig(String key, String val){
        getCacheService().set(CacheEnum.config + key, val, 24*60*60, TimeUnit.SECONDS);
    }


}